package com.pubinfo.passbook.api.vat.impl;

import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.api.vat.ForwardVATService;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.model.dto.financial.InvoiceResult;
import com.pubinfo.passbook.common.model.dto.financial.QueryDetailsParam;
import com.pubinfo.passbook.common.model.dto.financial.Result;
import com.pubinfo.passbook.common.model.dto.witeoff.MedicineOnJobOrRetireDTO;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ForwardVATServiceImpl implements ForwardVATService {
  @Value("${post.url}")
  String postUrl;
  @Value("${post.http-port}")
  String httpPort;
  private String sendGet(String url) {
    //Unirest.setTimeouts(0, 0);
    log.info("访问url:" + url);
    HttpResponse<String> response = null;
    try {
      response = Unirest.get(url)
          .header("User-Agent", "apifox/1.0.0 (https://www.apifox.cn)")
          .header("Content-Type", "application/json")
          .header("Accept-Encoding", "gzip, deflate, br")
          .asString();
    } catch (UnirestException e) {
      throw new ServiceException("调用服务失败", e);
    }
    return response.getBody();
  }
  
  private String sendHttpGet(String url) {
    
    return sendGet(postUrl + ":" + httpPort + url);
  }
  
  private String sendPost(String s, String url) {
    //Unirest.setTimeouts(0, 0);
    HttpResponse<String> response = null;
    try {
      response = Unirest.post(url)
          .header("User-Agent", "apifox/1.0.0 (https://www.apifox.cn)")
          .header("Content-Type", "application/json")
          .header("Accept-Encoding", "gzip, deflate, br")
          .body(s)
          .asString();
    } catch (UnirestException e) {
      throw new ServiceException("调用服务失败", e);
    }
    return response.getBody();
  }
  
  private String sendHttpPost(String s, String url) {
    
    return sendPost(s, postUrl + ":" + httpPort + url);
  }
  
  @Override
  public JsonResult<InvoiceResult<Result>> detailTPassbook(String eInvoiceId) {
    
    String reply = sendHttpGet("/api/passbook/details/" + eInvoiceId);
    return JSONObject.parseObject(reply, JsonResult.class);
  }
  
  @Override
  public InvoiceResult<Result> detailTPassbook(String[] eInvoiceIds) {
    
    String reply = sendHttpPost(JSONObject.toJSONString(eInvoiceIds), "/api/passbook/detailsBatch");
    
    return JSONObject.parseObject(reply, InvoiceResult.class);
  }
  
  @Override
  public JsonResult<String> medicineOnJobAutoCreateWriteoff(MedicineOnJobOrRetireDTO jobDTO) {
    
    String reply = sendHttpPost(JSONObject.toJSONString(jobDTO), "/api/mss/medicineOnJobAutoCreateWriteoff");
    return JSONObject.parseObject(reply, JsonResult.class);
  }
  
  @Override
  public String getWriteoffBaseDataItem(String xml) {
    
    return sendHttpPost(xml, "/api/mss/getWriteoffBaseDataItem");
  }
  
  
  @Override
  public JsonResult<Boolean> changeStatusTask(String id) {
    
    String reply = sendHttpGet("/api/task/changeStatus/" + id);
    
    return JSONObject.parseObject(reply, JsonResult.class);
  }
  
  @Override
  public JsonResult<String> runTask(String id) {
    
    String reply = sendHttpGet("/api/task/run/" + id);
    
    return JSONObject.parseObject(reply, JsonResult.class);
  }
  
  @Override
  public JsonResult<String> removeBatchTask(String[] ids) {
    
    String reply = sendHttpPost(JSONObject.toJSONString(ids), "/api/task/removeBatch");
    
    return JSONObject.parseObject(reply, JsonResult.class);
  }
  
  @Override
  public InvoiceResult<Object> cancelLockedList(String[] eInvoiceIds) {
    
    String reply = sendHttpPost(JSONObject.toJSONString(eInvoiceIds), "/api/passbook/cancelLockedList");
    
    return JSONObject.parseObject(reply, InvoiceResult.class);
  }
  
  @Override
  public JSONObject detailsByParam(QueryDetailsParam detailsParam) {
    
    String reply = sendHttpPost(JSONObject.toJSONString(detailsParam), "/api/com/pubinfo/passbook/detailsByParam");
  
    return JSONObject.parseObject(reply);
  }
  
  
  @Override
  public JSONObject QueryTwiteoffInstance() {
    
    String reply = sendHttpGet("/api/com/pubinfo/passbook/test/list");
    
    return JSONObject.parseObject(reply);
  }
}
